package org.springblade.modules.admin.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;

import java.util.Date;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/9/8 8:34
 */
public class DateUtils {

	public final static Integer HOURS_MINUTE = 60;

	/**
	 * 分钟转为小时
	 *
	 * @param minuteStr 分钟数
	 * @return 数据集
	 */
	public static String minuteToHours(int minuteStr) {
		String resultStr = "";
		if (minuteStr != 0) {
			if (minuteStr / 60 == 0) {
				resultStr = "0时" + minuteStr % 60 + "分";
			} else {
				if (minuteStr % 60 == 0) {
					resultStr = minuteStr / 60 + "小时";
				} else {
					resultStr = (minuteStr / 60 + "小时" + minuteStr % 60 + "分");
				}
			}
		} else {
			resultStr = "0时0分";
		}
		return resultStr;
	}

	/**
	 * 算耗时
	 *
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static String chineseBetweenTime(Date beginDate, Date endDate) {
		if (beginDate == null || endDate == null) {
			return null;
		}

		StringBuilder timeStr = new StringBuilder();
		long second = DateUtil.between(beginDate, endDate, DateUnit.SECOND);
		if (second / 3600 > 0) {
			timeStr.append(second / 3600).append("时");
			second = second % 3600;
		}
		if (second / 60 > 0) {
			timeStr.append(second / 60).append("分");
			second = second % 60;
		}
		timeStr.append(second).append("秒");

		return timeStr.toString();
	}

	/**
	 * 格式：yyyy-m-dd (今天/明天/后天)
	 *
	 * @param date
	 * @return
	 */
	public static String chineseDate(Date date) {
		StringBuilder str = new StringBuilder(DateUtil.format(date, "yyyy-M-dd"));
		long day = DateUtil.betweenDay(DateUtil.date(), date, true);
		if (day == 0) {
			str.append(" (今天) ");
		} else if (day == 1) {
			str.append(" (明天) ");
		} else if (day == 2) {
			str.append(" (后天) ");
		}
		return str.toString() + DateUtil.format(date, "HH:mm");
	}

	/**
	 * 转换为格式：1h30min
	 *
	 * @param millisecond 毫秒
	 * @return 时间
	 */
	public static String millisecondToTime(Long millisecond) {
		// 判断是否为空
		if (millisecond == null) {
			return StringPool.EMPTY;
		}
		if (millisecond == 0L) {
			return "0min";
		}
		//转换小时
		long hours = millisecond / 1000 / 3600;
		//剩余毫秒
		millisecond = millisecond % (1000 * 3600);
		//转换分钟
		long minutes = millisecond / 1000 / 60;
		if (hours > 0) {
			return StringUtil.format("{}h{}min", hours, minutes);
		} else {
			return StringUtil.format("{}min", minutes);
		}
	}


	/**
	 * 获取当前数据的结束时间(秒级别)
	 * 比如：2024-11-26 08:45:12
	 * 返回 2024-11-26 08:46:00
	 *
	 * @param date 时间
	 * @return 数据集
	 */
	public static Long getNowDateEnd(Date date) {
		return DateUtil.parse(DateUtil.format(new Date(date.getTime() + 60000), "yyyy-MM-dd HH:mm") + " 00").getTime();
	}


	public static void main(String[] args) {
		System.out.println(millisecondToTime(16657000L));
	}
}
